package com.zone.trigger.listener;

import com.alibaba.fastjson.JSONObject;
import com.zone.domain.award.event.UserWinAwardEvent;
import com.zone.domain.award.model.entity.DistributeAwardEntity;
import com.zone.domain.award.service.IAwardService;
import com.zone.types.event.BaseMessage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author: zongzi
 * @description: 用户中奖消息处理者
 * @date: 2025/2/22
 */
@Slf4j
@Component
public class UserWinAwardConsumer {
    @Autowired
    private IAwardService awardService;


    @RabbitListener(queuesToDeclare = @Queue(value = "${spring.rabbitmq.topic.user_win_award}"))
    public void consumer(BaseMessage<UserWinAwardEvent> message) {
        log.info("用户中奖消费：message = {}", JSONObject.toJSONString(message));
        UserWinAwardEvent userWinAwardEvent = message.getData();
        // 处理发奖
        DistributeAwardEntity distributeAwardEntity = new DistributeAwardEntity();
        distributeAwardEntity.setUserId(userWinAwardEvent.getUserId());
        distributeAwardEntity.setActivityId(userWinAwardEvent.getActivityId());
        distributeAwardEntity.setAwardId(userWinAwardEvent.getAwardId());
        distributeAwardEntity.setAwardConfig(userWinAwardEvent.getAwardConfig());
        distributeAwardEntity.setOrderId(userWinAwardEvent.getOrderId());
        awardService.distribute(distributeAwardEntity);
    }
}